package com.zyicoding.myfactory.service.impl;

import com.zyicoding.myfactory.dao.EmployeeMapper;
import com.zyicoding.myfactory.pojo.Employee;
import com.zyicoding.myfactory.service.EmployeeService;
import com.zyicoding.myfactory.util.CreatName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public List<Employee> selectAllEmployee() {
        return employeeMapper.selectList(null);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean deleteEmployeeById(int id) {
        int deleteResult = employeeMapper.deleteById(id);
        return deleteResult > 0;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Boolean insertEmployee() {
        Employee employee = new Employee();
        employee.setJobid(new Random().nextInt(3) + 1);
        employee.setName(CreatName.getName());
        employee.setSalary(Double.valueOf(new Random().nextInt(30000)));
        return employeeMapper.insert(employee) > 0;
    }
}
